import datetime
import time

from rest_framework import serializers

from apps.UploadFile.models import *

# 文本信息 序列化器
class Text_UploadSerializers(serializers.Serializer):
    username = serializers.CharField(label="用户",max_length=32,allow_null=True,allow_blank=True)
    time = serializers.DateTimeField(label="时间")
    information = serializers.CharField(label="文本信息",max_length=64,allow_null=True,allow_blank=True)
    ip = serializers.CharField(label="用户ip",max_length=64,allow_null=True,allow_blank=True)

    class Meta:
        model = Text_uploadModel
        fields = '__all__'

    # 数据认证
    def validated_information(self,value):
        if value != None:
            return value

    # 将数据保存到数据库中
    def create(self, validated_data):
        data = Text_uploadModel(**validated_data)
        data.save()
        print("文件保存成功")
        return validated_data

# 图片信息 序列化器
class Image_UploadSerializers(serializers.Serializer):
    username = serializers.CharField(label="用户名", max_length=32, allow_null=True,allow_blank=True)
    time = serializers.DateTimeField(label="插入时间")
    image = serializers.ImageField(label="图片",default=None,allow_null=True)
    image_name = serializers.CharField(label="图片名称", max_length=64,default=None)
    ip = serializers.CharField(label="ip地址", max_length=64, allow_null=True,allow_blank=True)
    is_delete = serializers.BooleanField(default=False, label='逻辑删除', help_text='逻辑删除')

    class Meta:
        model = Imgs_uploadModel
        fields = '__all__'

    # 数据认证
    def validate(self, attrs):
        return attrs


    # 将数据保存到数据库中
    def create(self, validated_data):
        data = Imgs_uploadModel(**validated_data)
        data.save()
        print("图片保存成功")
        return validated_data
